package breeze.pixel.weather.weather_forecast.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;


import java.util.List;

import breeze.pixel.weather.BaseView;
import breeze.pixel.weather.R;
import breeze.pixel.weather.adapter.weather.DailyWeatherAdapter;
import breeze.pixel.weather.apps_util.views.mtoolbar.MToolbar;
import breeze.pixel.weather.weather_forecast.model.ForecastAdapter;
import breeze.pixel.weather.weather_forecast.presenter.IWeatherForecastPresenter;
import breeze.pixel.weather.weather_forecast.presenter.WeatherForecastPresenter;

public class WeatherForecastView extends BaseView implements IWeatherForecastView{

    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather_forecast_view);
        init();
        initData();
    }

    @Override
    public void init() {
        ToolBarOnClick onClick = new ToolBarOnClick();
        MToolbar toolbar = find(R.id.forecast_toolbar);
        toolbar.setNavigation(R.mipmap.icon_back, onClick);
        toolbar.setSubTitle(getIntent().getStringExtra("cityName"));
        recyclerView = find(R.id.forecast_recyclerview);
    }

    @Override
    public void initData() {
        String location = getIntent().getStringExtra("cityID");
        IWeatherForecastPresenter presenter = new WeatherForecastPresenter(this, this);
        presenter.getWeatherForecast(location);
    }

    @SuppressLint("WrongConstant")
    @Override
    public void setListData(List<DailyWeatherAdapter.DailyBean> datas) {
        LinearLayoutManager linearMnager = new LinearLayoutManager(this);
        linearMnager.setOrientation(OrientationHelper.VERTICAL);
        recyclerView.setLayoutManager(linearMnager);
        ForecastAdapter adapter = new ForecastAdapter(this, datas);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListenr(new ForecastAdapter.onItemClickListner() {
            @Override
            public void onItemClick(DailyWeatherAdapter.DailyBean bean) {
                ShowForecastView.setData(bean);
                startActivity(new Intent(WeatherForecastView.this,ShowForecastView.class).putExtra("cityName", getIntent().getStringExtra("cityName")));
            }
        });
    }

    class ToolBarOnClick implements View.OnClickListener{
        @SuppressLint("NonConstantResourceId")
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.viewtoolbar_img_negative) {
                finish();
            } else {
                throw new IllegalStateException("Unexpected value: " + v.getId());
            }
        }
    }
}